package zabbix

import (
	"encoding/json"
	"errors"
)

func DiscoveryruleCopyData() (string,map[string]interface{}) {
	method := "discoveryrule.copy"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "discoveryids": [
            "27426"
        ],
        "hostids": [
            "10196",
            "10197"
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func DiscoveryruleCreateData() (string,map[string]interface{}) {
	method := "discoveryrule.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "Mounted filesystem discovery",
        "key_": "vfs.fs.discovery",
        "hostid": "10197",
        "type": "0",
        "interfaceid": "112",
        "delay": "30s"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func DiscoveryruleDeleteData() (string,map[string]interface{}) {
	method := "discoveryrule.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "27665",
        "27668"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func DiscoveryruleGetData() (string,map[string]interface{}) {
	method := "discoveryrule.get"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "output": "extend",
        "hostids": "10202"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func DiscoveryruleUpdateData() (string,map[string]interface{}) {
	method := "discoveryrule.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "itemid": "22450",
        "filter": {
            "evaltype": 1,
            "conditions": [
                {
                    "macro": "{#FSTYPE}",
                    "value": "@File systems for discovery"
                }
            ]
        }	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func DiscoveryruleCopy(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := DiscoveryruleCopyData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


func DiscoveryruleCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := DiscoveryruleCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


func DiscoveryruleDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := DiscoveryruleDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


func DiscoveryruleGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := DiscoveryruleGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


func DiscoveryruleUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := DiscoveryruleUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



